package handler

import (
	"github.com/labstack/echo/v4"
	"log"
	"strconv"
	"time"
	"ulcdkey/db"
	"ulcdkey/tool"
)

func QueryLog(c echo.Context) (err error) {
	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)

	nLog := new(db.ServerLog)

	startTime := c.FormValue("startTime")
	endTime := c.FormValue("endTime")
	userId := c.FormValue("userId")
	module := c.FormValue("module")
	platform := c.FormValue("platform")
	actionType := c.FormValue("actionType")

	as, err := nLog.QueryLog(startTime, endTime, actionType, module, platform, userId)
	if err != nil {
		log.Println("QueryLog err:", err)
		r.Code = -1
		r.Msg = "查询日志失败,err=" + err.Error()
		return c.JSON(200, r)
	}

	r.Data = as

	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")
	return c.JSON(200, r)
}

// 查询日志详情
func QueryLogDetail(c echo.Context) (err error) {

	var r ResponseResult
	t := time.Now()
	uid := tool.GetUid(c)

	nLog := new(db.ServerLog)
	logId, err := strconv.Atoi(c.FormValue("logId"))
	if err != nil {
		r.Code = -1
		r.Msg = "logId参数错误"
		return c.JSON(200, r)
	}

	a, err := nLog.QueryLogDetail(logId)
	if err != nil {
		log.Println("QueryLogDetail:", err)
		r.Code = -1
		r.Msg = "查询日志详情失败,err=" + err.Error()
		return c.JSON(200, r)
	}

	r.Data = a

	log.Println(c.Request().URL, "uid =", uid, "【耗时:", time.Now().Sub(t).Seconds(), "s】")

	return c.JSON(200, r)
}
